<?php
namespace app\admin\controller;
use app\admin\model\IndexTableModel;
use app\admin\model\JoinTableModel;
use app\admin\controller\Cloudaccountwithdraw;
use think\Db;
use think\Request;
use app\common\model\User;
use app\common\model\UserLevel;
use app\common\validate\UserValidate;

require_once (__DIR__.'/../../../vendor/ReidsLock.class.php');
class TeamController extends Controller
{
    /**
     * 团队全体充值详情
     * User:@hang
     */
    public function getallinfo(Request $request)
    {
        $param = $request->param();
        $teamArr = [];//团队长ID数组
        $dj_member_id = 0;
        if (isset($param['member_id']) && !empty($param['member_id'])) {
            $dj_member_id = $param['member_id'];
            $teamArr[] = $dj_member_id;
        }else{
            $Common = New Common();
            $htgldteamidstr = $Common->getConfigValue('htgldteamidstr');
            if(!empty($htgldteamidstr)){
                $teamArr = explode('#',$htgldteamidstr);
            }
        }

        $timeword = '一共';
        $whehetime = '';
        $start_time = 0;$end_time = 0;
        if (isset($param['add_time']) && !empty($param['add_time'])) {
            $time_arr = explode(' - ', $param['add_time']);
            $start_time = strtotime($time_arr[0]);
            $end_time = strtotime($time_arr[1].'23:59:59');
            $whehetime = " and add_time >= $start_time AND add_time <= $end_time";
            $timeword = '【'.date('Y-m-d H:i:s',$start_time).' 至 '.date('Y-m-d H:i:s',$end_time).'】';
        }

        $arrs = array();
        if(!empty($teamArr)){
            foreach ($teamArr as $k=>$v){
                $info = [];
                $dj_member_id = $v;//团队长顶级用户ID
                //查询顶级用户下的所有用户
                $list = getFemaleSubordinates($dj_member_id);
                $xlist = $list['member_id_str'];
                $xlistsex = $list['member_sex_str'];

                $xstr = '';
                if(!empty($xlist)){
                    $xstr = implode(',',$xlist);
                }

                //团队下充值金额
                $recharge_money = DB::table('fa_recharge_stamp_order')->where("`member_id` in ({$xstr})".$whehetime)->sum('all_stamp_num');
                $recharge_money_word = $timeword."团队下充值：{$recharge_money} 元";
                $info['dj_member_id'] = $v;
                $info['recharge_money'] = $recharge_money;
                $info['recharge_money_word'] = $recharge_money_word;
                $arrs[] = $info;
            }
            // 自定义排序函数，$a 和 $b 代表数组中的两个元素
            usort($arrs, function ($a, $b) {
                // 这里根据 'age' 字段排序，从大到小
                return $b['recharge_money'] - $a['recharge_money'];
            });
        }

        //关键词，排序等赋值
        $this->assign($request->get());
        $this->assign([
            'data'  => $arrs,
            'start_time'  => $start_time,
            'end_time'  => $end_time,
        ]);
        return $this->fetch();
    }

    /**
     * 单个团队充值详情
     * User:@hang
     */
    public function getstamplist(Request $request)
    {
        $id = input('id');//筛选条件
        $arr = explode('#',$id);
        $dj_member_id = $arr[0];
        $start_time = $arr[1];
        $end_time = $arr[2];

        //查询顶级用户下的所有用户
        $list = getFemaleSubordinates($dj_member_id);
        $xlist = $list['member_id_str'];
        $xstr = '';
        if(!empty($xlist)){
            $xstr = implode(',',$xlist);
        }

        $param = $request->param();
        $whehe = [];
        if (isset($param['member_id']) && !empty($param['member_id'])) {
            $whehe[] = ['a.member_id','=',$param['member_id']];
        }

        $whehetime = '';$tjwheretime = '';$timeword = '全部';
        if (!empty($start_time) && !empty($end_time)) {
            $whehetime = "a.add_time >= $start_time AND a.add_time <= $end_time";
            $tjwheretime = " and cz.add_time >= $start_time AND cz.add_time <= $end_time";
            $timeword = '【'.date('Y-m-d H:i:s',$start_time).' 至 '.date('Y-m-d H:i:s',$end_time).'】';
        }

        $data = Db::table('fa_recharge_stamp_order')
            ->alias('a')
            ->join("fa_member b",'a.member_id = b.member_id','LEFT')
            ->join("fa_member f",'b.me_parent = f.member_id','LEFT')
            ->where("a.member_id in($xstr)")
            ->where($whehe)
            ->where($whehetime)
            ->where("b.member_id is not null")
            ->field("a.id,a.add_time,b.member_id as cz_member_id,b.me_uid,b.me_phone,b.me_sex,b.me_nick_name,b.me_status,b.phone_agent,b.me_bind_type,f.member_id as f_member_id,f.me_uid as f_me_uid,f.me_phone as f_me_phone,f.me_sex as f_me_sex,f.me_nick_name as f_me_nick_name,f.me_status as f_me_status,(select sum(cz.pay_money) from fa_recharge_stamp_order cz where cz.member_id = a.member_id".$tjwheretime.") as cz_all_money,(select count(cz.id) from fa_recharge_stamp_order cz where cz.member_id = a.member_id".$tjwheretime.") as cz_all_num")
            ->group("a.member_id")
            ->order('cz_all_money desc,a.id desc')
            ->paginate($this->admin['per_page'],false,['query'=>request()->param()]);

        $adminid = $this->uid;
        $data->each(function ($v, $k)use($adminid) {
            //只有超管能看全部手机号
            if($adminid != 1){
                $v['me_phone'] = hide_phone_number($v['me_phone']);
            }
            switch($v['me_bind_type']){
                case '0': $v['me_bind_type_word'] = '未绑定上级';break;
                case '1': $v['me_bind_type_word'] = '扫码绑定';break;
                case '2': $v['me_bind_type_word'] = '充值绑定';break;
            }
            switch($v['me_sex']){
                case '0': $v['me_sex_word'] = '未设置';break;
                case '1': $v['me_sex_word'] = '男';break;
                case '2': $v['me_sex_word'] = '女';break;
            }
            if(!empty(strpos($v['me_phone'], 'zx'))){
                $v['me_phone'] = explode('zx',$v['me_phone'])[0].'（已注销）';
                $v['me_nick_name'] = explode('zx',$v['me_nick_name'])[0].'（已注销）';
            }

            if(!empty($v['f_member_id'])){
                switch($v['f_me_sex']){
                    case '0': $v['f_me_sex_word'] = '未设置';break;
                    case '1': $v['f_me_sex_word'] = '男';break;
                    case '2': $v['f_me_sex_word'] = '女';break;
                }
                if(!empty(strpos($v['f_me_phone'], 'zx'))){
                    $v['f_me_phone'] = explode('zx',$v['f_me_phone'])[0].'（已注销）';
                    $v['f_me_nick_name'] = explode('zx',$v['f_me_nick_name'])[0].'（已注销）';
                }
            }
            return $v;
        });

        //关键词，排序等赋值
        $this->assign($request->get());
        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
            'id' => $id,
            'timeword' => $timeword,
        ]);
        return $this->fetch();
    }


    /**
     * 每日收益奖励日志
     * User:@hang
     */
    public function getactivitylog(Request $request)
    {
        $param = $request->param();
        $whehe = [];
        if (isset($param['member_id']) && !empty($param['member_id'])) {
            $whehe[] = ['a.member_id','=',$param['member_id']];
        }
        if (isset($param['d_member_id']) && !empty($param['d_member_id'])) {
            $whehe[] = ['a.d_member_id','=',$param['d_member_id']];
        }

        $whehetime = '';
        if (isset($param['add_time']) && !empty($param['add_time'])) {
            $whehe[] = ['a.jl_day_time','=',$param['add_time']];
        }

        $data = Db::table('zz_activity_log')
            ->alias('a')
            ->join("fa_member b",'a.member_id = b.member_id','LEFT')
            ->join("fa_member f",'b.me_parent = f.member_id','LEFT')
            ->where($whehe)
            ->where($whehetime)
            ->field("a.*,b.me_uid,b.me_phone,b.me_sex,b.me_nick_name,b.me_status,b.phone_agent,b.me_bind_type,f.member_id as f_member_id,f.me_uid as f_me_uid,f.me_phone as f_me_phone,f.me_sex as f_me_sex,f.me_nick_name as f_me_nick_name,f.me_status as f_me_status")
            ->order('a.id desc')
            ->paginate($this->admin['per_page'],false,['query'=>request()->param()]);
        $adminid = $this->uid;
        $data->each(function ($v, $k)use($adminid) {
            //只有超管能看全部手机号
            if($adminid != 1){
                $v['me_phone'] = hide_phone_number($v['me_phone']);
            }
            switch($v['me_sex']){
                case '0': $v['me_sex_word'] = '未设置';break;
                case '1': $v['me_sex_word'] = '男';break;
                case '2': $v['me_sex_word'] = '女';break;
            }
            if(!empty(strpos($v['me_phone'], 'zx'))){
                $v['me_phone'] = explode('zx',$v['me_phone'])[0].'（已注销）';
                $v['me_nick_name'] = explode('zx',$v['me_nick_name'])[0].'（已注销）';
            }
            if(!empty($v['f_member_id'])){
                switch($v['f_me_sex']){
                    case '0': $v['f_me_sex_word'] = '未设置';break;
                    case '1': $v['f_me_sex_word'] = '男';break;
                    case '2': $v['f_me_sex_word'] = '女';break;
                }
                if(!empty(strpos($v['f_me_phone'], 'zx'))){
                    $v['f_me_phone'] = explode('zx',$v['f_me_phone'])[0].'（已注销）';
                    $v['f_me_nick_name'] = explode('zx',$v['f_me_nick_name'])[0].'（已注销）';
                }
            }
            return $v;
        });

        $ruleList = Db::table('zz_activity_rule')->where("`c_member_id` = -1")->select();
        $rulestr = '';
        if(!empty($ruleList)){
            foreach ($ruleList as $k=>$v){
                $rulestr .= '【当天收益满'.$v['m_money'].'，奖励'.$v['jl_money'].'】';
            }
        }

        //关键词，排序等赋值
        $this->assign($request->get());
        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
            'rulestr' => $rulestr,
        ]);
        return $this->fetch();
    }























}
